package com.cheetah.cloud2020.service;

import com.cheetah.cloud2020.bean.ResponseResult;
import com.cheetah.cloud2020.config.SpringTaskMgr;
import com.cheetah.cloud2020.entity.SystemJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class RibbonJobService {
    @Autowired
    private RestTemplate restTemplate;
    public ResponseResult addCron(SystemJob systemJob) {
        if (systemJob.isDelete()) {
            return ResponseResult.fail();
        }
        SpringTaskMgr.addCronTask(systemJob.getId(), () -> {
            if ("GET".equalsIgnoreCase(systemJob.getMethod())) {
                restTemplate.getForObject(systemJob.getUrl(), String.class);
            }
            if ("POST".equalsIgnoreCase(systemJob.getMethod())) {
                restTemplate.postForObject(systemJob.getUrl(), null, String.class);
            }
        }, systemJob.getCron());
        return ResponseResult.ok("");
    }
}
